package com.vidyu.businesslayer.businesscomponents.trackingmerger.impl;

import com.vidyu.businesslayer.businesscomponents.trackingmerger.MergeLoopWrapper;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 * User: eyal sadeg
 * Date: 01/03/12
 * Time: 16:13   eyal sadeh
 * Wrapper for fetch loop using quartz.
 */
@Component
public class MergeLoopWrapperImpl extends QuartzJobBean implements MergeLoopWrapper {

    private static final Logger logger = Logger.getLogger(MergeLoopWrapperImpl.class);

    @Override
    public void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
            TrackingMergerImpl obj = applicationContext.getBean(TrackingMergerImpl.class);
            obj.executeLoop();
        }  catch (JobExecutionException je) {
            logger.fatal("Exception in MergeLoopWrapperImpl", je);
            throw je;
        }   catch (Exception e) {
            logger.fatal("Exception in MergeLoopWrapperImpl", e);
            throw new RuntimeException(e);
        }
    }
}
